﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.BLL.UserProfileManagement;
using Microsoft.SharePoint;

namespace MS.InformationManagement.Presenter.GestionDeUsuarios
{
    public class GestionDeUsuariosPresenter : BasePresenter<IGestionDeUsuariosView>
    {
        #region Constructors

        public GestionDeUsuariosPresenter(IGestionDeUsuariosView view) : base(view) { }

        #endregion

        #region Methods

        public void HandleLoad()
        {
            View.SelectedUser = MS.InformationManagement.Support.PeopleHelper.GetUserById(View.CurrentId);
            View.CedulaUsuario = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_CEDULA, View.SelectedUser.LoginName);
            View.ListSubSecciones = new MS.InformationManagement.BLL.TRDSelector.TRDSelector().GetSecciones();
            View.NombreUsuario = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_FIRST_NAME, View.SelectedUser.LoginName);
            View.ApellidoUsuario = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_LAST_NAME, View.SelectedUser.LoginName);
            View.TelefonoUsuario = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_WORK_PHONE, View.SelectedUser.LoginName);
            View.UbicacionUsuario = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_OFFICE_lOCATION, View.SelectedUser.LoginName);
            View.EmailUsuario = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_EMAIL, View.SelectedUser.LoginName);
            View.DepartamentoUsuario = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_DEPARTMENT, View.SelectedUser.LoginName);
            View.UserPerfil = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_USER_PERFIL, View.SelectedUser.LoginName);
            View.AllPerfiles = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().GetPerfiles();
            View.HaveLibrary = MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL.GetLibrary(View.SelectedUser);
            View.IdPerfil = MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL.GetIdPerfilByName(View.UserPerfil);
            View.ListRoleByPerfil = MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL.LoadRolesProfile(View.IdPerfil);

            if (string.IsNullOrEmpty(View.NombreUsuario) || string.IsNullOrEmpty(View.EmailUsuario))
                throw new Exception("El usuario no esta correctamente configurado en el servicio de perfiles de usuario de SharePoint");

            //string TermPais = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().getProperty(GlobalConstants.UP_PAIS, View.SelectedUser.LoginName);
            //if (!string.IsNullOrEmpty(TermPais))
            //    View.UserPais = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_COUNTRY, TermPais);

            View.Show();

        }

        public void HandleUpdate()
        {
            
            View.SucceedUpdate = new MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL().updatePropertys(View.SelectedUser, View.CedulaUsuario,
                 View.TelefonoUsuario, View.UbicacionUsuario, View.DepartamentoUsuario, View.UserPerfil, View.Observaciones);

            View.Result();
        }

        public void RequestRoles()
        {
            View.IdPerfil = MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL.GetIdPerfilByName(View.UserPerfil);
            View.ListRoleByPerfil = MS.InformationManagement.BLL.GestionDeUsuarios.GestionDeUsuariosBLL.LoadRolesProfile(View.IdPerfil);
            View.UpdateRoles();
        }        

        #endregion
    }
}
